//
//  BirthdayModel.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/1/27.
//

#import "BirthdayModel.h"

@implementation BirthdayModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.createdDate = [NSDate date];
        self.isEnabled = YES;
        self.notifyOnDay = YES;
        self.notifyOneDayBefore = YES;
        self.notifyOneWeekBefore = NO;
    }
    return self;
}

- (NSInteger)daysUntilBirthday {
    NSDate *nextBirthday = [self nextBirthdayDate];
    NSDate *today = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:today toDate:nextBirthday options:0];
    
    return components.day;
}

- (NSDate *)nextBirthdayDate {
    // 这里简化处理，实际应该使用农历转换库
    // 暂时返回当前年份的生日日期
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
    components.month = self.lunarMonth;
    components.day = self.lunarDay;
    
    NSDate *birthday = [calendar dateFromComponents:components];
    
    // 如果今年的生日已经过了，返回明年的生日
    if ([birthday compare:[NSDate date]] == NSOrderedAscending) {
        components.year += 1;
        birthday = [calendar dateFromComponents:components];
    }
    
    return birthday;
}

- (NSDate *)solarBirthdayDate {
    // 简化处理，返回农历对应的公历日期
    return [self nextBirthdayDate];
}

- (NSString *)lunarMonthName {
    NSArray *monthNames = @[@"", @"正月", @"二月", @"三月", @"四月", @"五月", @"六月",
                           @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月"];
    if (self.lunarMonth >= 1 && self.lunarMonth <= 12) {
        return monthNames[self.lunarMonth];
    }
    return @"";
}

- (NSString *)lunarDayName {
    NSArray *dayNames = @[@"", @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
                         @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
                         @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"];
    if (self.lunarDay >= 1 && self.lunarDay <= 30) {
        return dayNames[self.lunarDay];
    }
    return @"";
}

- (NSDictionary *)toDictionary {
    return @{
        @"name": self.name ?: @"",
        @"relationship": self.relationship ?: @"",
        @"lunarMonth": @(self.lunarMonth),
        @"lunarDay": @(self.lunarDay),
        @"birthYear": @(self.birthYear),
        @"note": self.note ?: @"",
        @"isEnabled": @(self.isEnabled),
        @"notifyOnDay": @(self.notifyOnDay),
        @"notifyOneDayBefore": @(self.notifyOneDayBefore),
        @"notifyOneWeekBefore": @(self.notifyOneWeekBefore),
        @"createdDate": self.createdDate ?: [NSDate date]
    };
}

+ (instancetype)fromDictionary:(NSDictionary *)dict {
    BirthdayModel *model = [[BirthdayModel alloc] init];
    model.name = dict[@"name"];
    model.relationship = dict[@"relationship"];
    model.lunarMonth = [dict[@"lunarMonth"] integerValue];
    model.lunarDay = [dict[@"lunarDay"] integerValue];
    model.birthYear = [dict[@"birthYear"] integerValue];
    model.note = dict[@"note"];
    model.isEnabled = [dict[@"isEnabled"] boolValue];
    model.notifyOnDay = [dict[@"notifyOnDay"] boolValue];
    model.notifyOneDayBefore = [dict[@"notifyOneDayBefore"] boolValue];
    model.notifyOneWeekBefore = [dict[@"notifyOneWeekBefore"] boolValue];
    
    // 处理日期
    id dateObj = dict[@"createdDate"];
    if ([dateObj isKindOfClass:[NSDate class]]) {
        model.createdDate = dateObj;
    } else {
        model.createdDate = [NSDate date];
    }
    
    return model;
}

@end 